<?php

namespace App\Http\Controllers\Wechat\WechatServiceAccount;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\WechatServiceAccountAddress;

class AddressController extends Controller
{
    public function index()
    {
        $user = Auth::guard('wechat')->user();
        $addresses = WechatServiceAccountAddress::where('user_id', $user->id)->orderByDesc('is_default')->latest()->get();
        return view('wechat.service_account.addresses', compact('addresses'));
    }

    public function create()
    {
        return view('wechat.service_account.address_create');
    }

    public function store(Request $request)
    {
        $user = Auth::guard('wechat')->user();
        
        $request->validate([
            'consignee' => 'required|string|max:255',
            'mobile' => 'required|string|max:20',
            'province' => 'required|string|max:255',
            'city' => 'required|string|max:255',
            'district' => 'nullable|string|max:255',
            'address' => 'required|string|max:255',
        ]);

        // 如果这是用户添加的第一个地址，设置为默认地址
        $isDefault = WechatServiceAccountAddress::where('user_id', $user->id)->count() == 0;

        $address = WechatServiceAccountAddress::create([
            'user_id' => $user->id,
            'consignee' => $request->consignee,
            'mobile' => $request->mobile,
            'province' => $request->province,
            'city' => $request->city,
            'district' => $request->district,
            'address' => $request->address,
            'is_default' => $isDefault,
        ]);

        return redirect()->route('wechat.service.account.addresses')->with('success', '地址添加成功');
    }

    public function edit($id)
    {
        $user = Auth::guard('wechat')->user();
        $address = WechatServiceAccountAddress::where('user_id', $user->id)->findOrFail($id);
        return view('wechat.service_account.address_edit', compact('address'));
    }

    public function update(Request $request, $id)
    {
        $user = Auth::guard('wechat')->user();
        $address = WechatServiceAccountAddress::where('user_id', $user->id)->findOrFail($id);

        $request->validate([
            'consignee' => 'required|string|max:255',
            'mobile' => 'required|string|max:20',
            'province' => 'required|string|max:255',
            'city' => 'required|string|max:255',
            'district' => 'nullable|string|max:255',
            'address' => 'required|string|max:255',
        ]);

        $address->update([
            'consignee' => $request->consignee,
            'mobile' => $request->mobile,
            'province' => $request->province,
            'city' => $request->city,
            'district' => $request->district,
            'address' => $request->address,
        ]);

        return redirect()->route('wechat.service.account.addresses')->with('success', '地址更新成功');
    }

    public function destroy($id)
    {
        $user = Auth::guard('wechat')->user();
        $address = WechatServiceAccountAddress::where('user_id', $user->id)->findOrFail($id);
        
        // 如果要删除的是默认地址，需要先设置另一个地址为默认（如果有其他地址的话）
        if ($address->is_default && WechatServiceAccountAddress::where('user_id', $user->id)->count() > 1) {
            $nextAddress = WechatServiceAccountAddress::where('user_id', $user->id)->where('id', '!=', $address->id)->first();
            if ($nextAddress) {
                $nextAddress->update(['is_default' => true]);
            }
        }
        
        $address->delete();

        return redirect()->route('wechat.service.account.addresses')->with('success', '地址删除成功');
    }

    public function setDefault($id)
    {
        $user = Auth::guard('wechat')->user();
        $address = WechatServiceAccountAddress::where('user_id', $user->id)->findOrFail($id);

        // 将该用户的所有其他地址设为非默认
        WechatServiceAccountAddress::where('user_id', $user->id)->update(['is_default' => false]);

        // 将当前地址设为默认
        $address->update(['is_default' => true]);

        return redirect()->route('wechat.service.account.addresses')->with('success', '默认地址设置成功');
    }
}